function WP_GetPlayerDirection()
	local obj;
	do
		local t = {Minimap:GetChildren()};
		for k, v in pairs(t) do
			if v:IsObjectType("Model") and not v:GetName() then
				local model = v:GetModel():lower()
				if model:match("interface\\minimap\\minimaparrow") then 
					obj = v; break;
				end
			end
		end
	end
	if not obj then return; end

	WP_GetPlayerDirection = function() 
		if GetCVar("rotateMinimap") ~= "0" then
			return (MiniMapCompassRing:GetFacing() * -1)
		else
			return obj:GetFacing(); 
		end
	end
	return WP_GetPlayerDirection();
end 

function WP_GetDistance(x,y)
	local px,py = GetPlayerMapPosition("player")
	local dx = px - x
	local dy = py - y
	local d = math.sqrt(dx^2 + dy^2)
	return d
end 

function WP_GetDirection(x,y)
	local px,py = GetPlayerMapPosition("player")
	local pdir = WP_GetPlayerDirection()
	local pdis = WP_GetDistance(x,y)
	if px < x and py < y then
		dir = math.pi*0.5 - math.atan((y-py)/(x-px)) + math.pi - pdir
	elseif px < x and py > y then
		dir = math.atan((py-y)/(x-px)) + math.pi*1.5 - pdir
	elseif px > x and py > y then
		dir = math.pi*0.5 - math.atan((py-y)/(px-x)) - pdir
	else
		dir = math.atan((y-py)/(px-x)) + math.pi*0.5 - pdir
	end
	
	if dir < 0 then dir = 2*math.pi + dir end
	return dir
end 

function WP_SetArrow(x,y)
	local angle = WP_GetDirection(x,y);
	local dis = WP_GetDistance(x,y);
	
	if dis < 0.001 and WP_TomTom then
		WP_CheckCoordsEvent()
	elseif dis < 0.001 and WP_TopFrame_Arrow:IsVisible() then
		WP_TopFrame_Arrow:Hide()
		WP_TopFrame_Arrival:Show()
		WP_CheckCoordsEvent()
	else
		if dis > 0.001 and WP_TopFrame_Arrival:IsVisible() then WP_TopFrame_Arrival:Hide();end
		
		if (angle < 0.15 or angle > 6.133) and WP_TopFrame_Arrow:GetTexture() == "Interface\\Minimap\\ROTATING-MINIMAPARROW" then 
			WP_TopFrame_Arrow:SetTexture("Interface\\Minimap\\ROTATING-MINIMAPGUIDEARROW")
		 elseif (angle > 0.15 and angle < 6.133) and WP_TopFrame_Arrow:GetTexture() == "Interface\\Minimap\\ROTATING-MINIMAPGUIDEARROW" then
			WP_TopFrame_Arrow:SetTexture("Interface\\Minimap\\ROTATING-MINIMAPARROW")
		end
	end
	WP_TopFrame_Arrow:SetRotation(angle,0.5,1);
	
end 